' This file is part of the SunDLL.  The SunDLL is free software: you can
' redistribute it and/or modify it under the terms of the GNU General Public
' License as published by the Free Software Foundation, version 2.
'
' This program is distributed in the hope that it will be useful, but WITHOUT
' ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
' FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
' details.
'
' You should have received a copy of the GNU General Public License along with
' this program; if not, write to the Free Software Foundation, Inc., 51
' Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'
' Copyright David R. Pratten
'#Compile Exe
'#Include "MD5.inc"

Function RC4like(toEnc As String, wKeystring As String) As String
  Dim s(0 To 255)   As Long
  Dim k(0 To 255)   As Long
  Local i           As Long
  Local j           As Long
  Local t           As Long
  Local k           As Long
  Local c           As Long
  Local x           As Long
  Local Initi       As Long
  Local KeyCount    As Long
  Local KeyLen      As Long
  Local Initj       As Long
  Local RBuff       As String
  Local rBuffLen    As Long
  Local fromEncPos  As Long
  Local p           As Byte Ptr
  Local pp          As Byte Ptr
  Local FromEnc     As String

  KeyLen = Len(wKeystring)
  For Initi = 0 To 255
    s(Initi) = Initi
    Incr KeyCount
    k(Initi) = Asc(wKeystring, KeyCount)
    If KeyCount => KeyLen Then KeyCount = 0
  Next
  For Initi = 0 To 255
    Initj = (Initj + s(Initi) + k(Initi)) Mod 256
    Swap s(Initi), s(Initj)
  Next
  i = 0
  j = 0
  fromEnc = String$(Len(toEnc), 0)
fromEncPos = 0
p = StrPtr(toEnc)
pp = StrPtr(fromEnc)
  For x = 0 To Len(toEnc)-1
    c = @p[x]
    i = (i + 1) Mod 256
    j = (j + s(i)) Mod 256
    Swap s(i), s(j)
    t = (s(i) + s(j)) Mod 256
    k = s(t)

    @pp[fromEncPos] = c Xor k
    Incr fromEncPos
  Next
Function = fromEnc
End Function



Function S2HexS (InString As String) As String

Dim i As Long
Dim outstring As String

For i = 1 To Len(InString)
    Outstring = Outstring+Hex$(Asc(Instring,i),2)
Next
Function = outstring
End Function

Function HexS2S (InString As String) As String

Dim i As Long
Dim outstring As String

For i = 1 To Len(InString)/2
    Outstring = Outstring+Chr$(Val("&H"+Mid$(Instring,(i-1)*2+1,2) ))
Next
Function = outstring
End Function

Function StaticKey() As String
Function = MD5("Well if you got this far, walk right through.")
End Function

'Function PbMain() As Long



' MsgBox Hex2String(String2Hex("asdfa6666666dfkl;jasd"))
'End Function


